package com.kingtone.rongji.adapter;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.kingtone.rongji.OrderDetailActivity;
import com.kingtone.rongji.PayNoticeActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.SendFriendsActivity;
import com.kingtone.rongji.biz.Order;
import com.kingtone.rongji.tool.AppContasts;
import com.kingtone.rongji.tool.RongJiUtils;
import com.kingtone.rongji.tool.SharedPreferencesUtil;

@SuppressLint("ShowToast")
public class OrderAdapter extends BaseAdapter {
	private List<Order> list = new ArrayList<Order>();
	private Context context;
	LayoutInflater inflater=null;
	private Order order = null;
    public OrderAdapter(List<Order> list,Context context) {
    	this.context = context;
    	this.list = list;
        inflater=LayoutInflater.from(context);
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
    	View view;
		ViewHolder holder = null;
    	if(convertView != null) {
    		view = convertView;
			holder= (ViewHolder) view.getTag();
        }else
        {
        	view =inflater.inflate(R.layout.order_item, null);
    		holder= new ViewHolder();
    		holder.orderNo = (TextView) view.findViewById(R.id.order_no);
    		holder.orderStatus = (TextView) view.findViewById(R.id.order_status);
    		holder.orderDate = (TextView) view.findViewById(R.id.order_date);
    		holder.orderNum = (TextView) view.findViewById(R.id.order_product_num);
    		holder.orderCount = (TextView) view.findViewById(R.id.order_count);
    		holder.detailBtn = (Button)view.findViewById(R.id.order_detail);
    		holder.payBtn = (Button)view.findViewById(R.id.pay_right_now);
    		holder.sendBtn = (Button)view.findViewById(R.id.send_friends);
        }
    	view.setTag(holder);
        order = list.get(position);
        holder.detailBtn.setTag(position);
        holder.detailBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int index = (Integer)v.getTag();
				Order detailOrder = list.get(index);
				Intent intent = new Intent(context, OrderDetailActivity.class);
				intent.putExtra("orderNum", detailOrder.getOrderNumber());
				context.startActivity(intent);
			}
		});
      
        holder.payBtn.setTag(position);
        if(order.getOrderStatus() == 1) //待支付
		{
        	holder.payBtn.setVisibility(View.VISIBLE);
        	holder.sendBtn.setVisibility(View.GONE);
        	holder.payBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					int index = (Integer)v.getTag();
					Order payOrder = list.get(index);
					Intent intent = new Intent(context, PayNoticeActivity.class);
					String subject = context.getString(R.string.welcome_img) +"_"+ payOrder.getOrderNumber();
					intent.putExtra("orderNo", payOrder.getOrderNumber());
					intent.putExtra("subject", subject);
					String cutsomer = payOrder.getCustomerUserName();
					if(TextUtils.isEmpty(cutsomer))
					{
						cutsomer = SharedPreferencesUtil.getInstance(context).getString(AppContasts.USERNAME_KEY);
						
					}
					if(!TextUtils.isEmpty(cutsomer))
					{
						cutsomer  = cutsomer + "_";
					}
					intent.putExtra("body", cutsomer  + payOrder.getOrderNumber());
					intent.putExtra("payAmount", payOrder.getOrderPayAmount());
					context.startActivity(intent);
				}
			});
		}else
		{
			holder.payBtn.setVisibility(View.GONE);
			if(order.getOrderStatus() == 6) //送朋友
			{
				holder.sendBtn.setVisibility(View.VISIBLE);
				holder.sendBtn.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v)
					{
						//加载WebView
						Intent intent = new Intent(context, SendFriendsActivity.class);
						intent.putExtra("orderNum", order.getOrderNumber());
						context.startActivity(intent);
						
					}
				});
			}
		}
        holder.orderNo.setText(context.getString(R.string.order_text)+order.getOrderNumber());
        holder.orderDate.setText(RongJiUtils.revertDate(order.getOrderDate()));
        holder.orderStatus.setText(Order.getOrderStatus(order.getOrderStatus()));
        String[] productNameList = order.getProductNameList().split(",");
        if(productNameList != null && productNameList.length == 2)
        {
        	holder.orderNum.setText(productNameList[1]);
        }
        holder.orderCount.setText("￥"+order.getOrderPayAmount());
        return view;
    }

    private class ViewHolder
	{
		public TextView orderNo;
		public TextView orderStatus;
		public TextView orderDate;
		public TextView orderNum;
		public TextView orderCount;
		public Button detailBtn;
		public Button payBtn;
		public Button sendBtn;
	}
}
